const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    unitName: '',
    ownerIdno: '',
    ownerName: '',
    goodsDesc: '',
    startTime: '',
    endTime: '',
    startTimeLon: '',
    endTimeLon: '',
    goodsTime: '',
    goodsTimeLon: '',
    dataType: '2',
    originFiles: [],
    modalHidden: true
  },
  //表单提交
  formSubmit(e) {
    const { unitName, ownerIdno, ownerName, goodsDesc, startTime } = e.detail.value;
    if (!unitName || !ownerIdno || !ownerName
      || !goodsDesc || !startTime) {
      wx.showToast({
        title: '请填写必填项！！！',
        duration: 2000,
        icon: 'none', //图标有success、error、loading、none四种
        mask: true //显示透明蒙层 防止触摸穿透
      })
      return;
    }
    if (!(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(ownerIdno))) {
      wx.showToast({
        title: '身份证填写错误！！！',
        duration: 2000,
        icon: 'none', //图标有success、error、loading、none四种
        mask: true //显示透明蒙层 防止触摸穿透
      })
      return;
    }
    const multipleComp = this.selectComponent('#multiple');
    const { originFiles } = multipleComp.data;
    this.setData({
      unitName, ownerIdno, ownerName, goodsDesc, startTime,
      originFiles: originFiles,
      modalHidden: false
    });
  },
  //模态框取消
  modalCancel() {
    wx.showToast({
      title: '取消提交',
      icon: 'none'
    })
    this.setData({
      modalHidden: true,
    })
  },
  //模态框确定
  modalConfirm() {
    this.addDataReport();
    this.setData({
      modalHidden: true
    })
  },
  // 提交数据报备
  addDataReport() {
    var userAccount = wx.getStorageSync('userAccount')
    if (!userAccount) {
      wx.showToast({
        icon: "none",
        title: '当前用户未绑定手机号！',
      })
      return;
    }
    const originFilesTmp = this.data.originFiles;
    const files = [];
    originFilesTmp.forEach(function(item){
      files.push({
        fileName: item.name,
        fileUrl: item.url,
        fileSize: item.size
      })
    });
    wx.request({
      url: app.globalData.addDataReport,
      data: {
        unitName: this.data.unitName,
        ownerIdno: this.data.ownerIdno,
        ownerName: this.data.ownerName,
        goodsDesc: this.data.goodsDesc,
        startTime: this.data.startTimeLon,
        endTime: this.data.endTimeLon,
        goodsTime: this.data.goodsTimeLon,
        dataType: this.data.dataType,
        createUserCode: userAccount,
        files:files
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function (data2) {
        if (data2.statusCode == 200) {
          wx.showToast({
            icon: "none",
            title: '数据提交成功！',
          })
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            });
          }, 2000)
        } else {
          wx.showToast({
            icon: "none",
            title: '服务器异常，请稍后再试！',
          })
        }
      },
      fail: function (err) {
        console.log(err);
        wx.showToast({
          icon: "none",
          title: '数据提交失败，请重试！',
        })
      }
    })
  },
  onChange(e) {

  },
  onLoad: function (options) {
  },
  clickHandle(e) {
    const { key } = e.currentTarget.dataset;
    const { datetime, datetimeText } = e.detail;
    if (key === 'startTime' && datetimeText) {
      this.setData({
        startTime: datetimeText,
        startTimeLon: datetime
      });
      this.setData({
        goodsTime: datetimeText,
        goodsTimeLon: datetime
      });
    }
    if (key === 'endTime' && datetimeText) {
      this.setData({
        endTime: datetimeText,
        endTimeLon: datetime
      });
    }
  }
})